package com.sky.sky_server.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sky.sky_common.result.Result;
import com.sky.sky_pojo.dto.OrderReceiveDTO;
import com.sky.sky_pojo.dto.OrderCancelDTO;
import com.sky.sky_pojo.dto.OrderRejectionDTO;
import com.sky.sky_pojo.entity.OrderQueryPage;
import com.sky.sky_server.service.impl.OrderServiceImpl;

import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderServiceImpl orderServiceImpl;

    /**
     * 订单确认,接单
     * @param id
     */
    @PutMapping("/confirm")
    public Result confirm(@RequestBody OrderReceiveDTO id) {
        Long orderId = id.getId();
        log.info("确认订单,接单,订单id:{}", orderId);
        int res = orderServiceImpl.confirmOrder(orderId);
        if(res <= 0) 
            return Result.error("确认订单失败");
        return Result.success();
    }

    /**
     * 订单取消
     * @param id
     * @param cancelReason
     * @return
     */
    @PutMapping("/cancel")
    public Result cancel(@RequestBody OrderCancelDTO orderCancelDTO) {
        log.info("取消订单,订单id:{},取消原因:{}", orderCancelDTO.getId(), orderCancelDTO.getCancelReason());
        Long id = orderCancelDTO.getId();
        String cancelReason = orderCancelDTO.getCancelReason();
        int res = orderServiceImpl.cancelOrderForAdmin(id, cancelReason);
        if(res <= 0) 
            return Result.error("取消订单失败");
        return Result.success();
    }

    /**
     * 根据id查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public Result getOrderDetails(@PathVariable Long id) {
        log.info("根据id查询订单详情,订单id:{}", id);
        return Result.success(orderServiceImpl.getOrderDetail(id));
    }
    
    /**
     * 订单搜索
     */
    @GetMapping("/conditionSearch")
    public Result searchOrder(@Valid OrderQueryPage orderQueryPage) {
        log.info("订单搜索,查询条件:{}", orderQueryPage);
        return Result.success(orderServiceImpl.searchOrderForAdmin(orderQueryPage));
    }

    /**
     * 各个状态的订单数量统计
     * @param orderStatistics
     * @return
     */
    @GetMapping("/statistics")
    public Result statisticsOrder() {
        log.info("统计订单");
        return Result.success(orderServiceImpl.statisticsOrder());
    }

    /**
     * 拒单
     */
    @PutMapping("/rejection")
    public Result rejectOrder(@RequestBody OrderRejectionDTO orderRejectionDTO) {
        Long id = orderRejectionDTO.getId();
        String rejectionReason = orderRejectionDTO.getRejectionReason();
        log.info("拒绝订单 id: {}, 拒绝原因: {}", id, rejectionReason);
        int res = orderServiceImpl.rejectOrderForAdmin(id, rejectionReason);
        if(res <= 0)
            return Result.error("拒绝订单失败");
        return Result.success("拒绝订单成功");
    }


    /**
     * 派送订单
     */
    @PutMapping("/delivery/{id}")
    public Result deliveryOrder(@PathVariable Long id) {
        log.info("派送订单 id: {}", id);
        int res = orderServiceImpl.deliveryOrderForAdmin(id);
        if(res <= 0)
            return Result.error("派送订单失败");
        return Result.success("派送订单成功");
    }

    /**
     * 完成订单
     */
    @PutMapping("/complete/{id}")
    public Result completeOrder(@PathVariable Long id) {
        log.info("完成订单 id: {}", id);
        int res = orderServiceImpl.completeOrderForAdmin(id);
        if(res <= 0)
            return Result.error("完成订单失败");
        return Result.success("完成订单成功");
    }

}
